<template>
  <div>
    <el-card class='box-card'>
      <div slot='header' class='clearfix'>
        <el-button
          icon='el-icon-plus'
          type='primary'
          size='medium'
          @click='addMenu'
        >新增
        </el-button>
      </div>
      <div>
        <el-table
          :data='menuData'
          style='width: 100%;margin-bottom: 20px;'
          row-key='id'
          :default-expand-all='false'
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column
            v-for='(col,index) in columns'
            :key='index'
            :prop='col.prop'
            :label='col.label'
            :sortable='col.sortable'
            :width='col.width'
          />
          <el-table-column v-if='menuData.length' align='center' width='120' label='操作'>
            <template slot-scope='scope'>
              <el-button
                size='small'
                type='text'
                @click='handleEdit(scope.row)'>编辑
              </el-button>
              <span style='color: rgba(126,128,129,0.23);user-select: none;'> | </span>
              <el-dropdown
                class='fx-dropdown'
                @command='handleCommand'
              >
                <span class='el-dropdown-link'>
                  更多<i class='el-icon-arrow-down el-icon--right' />
                </span>
                <el-dropdown-menu slot='dropdown'>
                  <el-dropdown-item :command='{cmd: "details", row: scope.row}'>详情</el-dropdown-item>
                  <el-dropdown-item :command='{cmd: "addChildren",row: scope.row}'>添加下级</el-dropdown-item>
                  <el-dropdown-item :command='{cmd: "deleteOne",row: scope.row}'>删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <FxMenuDrawer
      v-if='formData && menuInfo'
      @closeDrawer='closeDrawer'
      :title='drawerOpt.title'
      :dialog='drawerOpt.dialog'
      :direction='drawerOpt.direction'
      :disabled-submit='drawerOpt.disabledSubmit'
      :form-data='formData'
      :menus='menuInfo'
    />
  </div>
</template>

<script>
import FxMenuDrawer from '@/pages/admin/components/FxMenuDrawer'
import Sys_AdminService from '@/service/admin/Sys_AdminService'

export default {
  name: 'MenuList',
  components: {
    FxMenuDrawer
  },
  data() {
    return {
      columns: [
        {
          prop: 'title',
          label: '菜单名称',
          sortable: false,
          width: ''
        },
        {
          prop: 'icon',
          label: '图标',
          sortable: false,
          width: ''
        },
        {
          prop: 'path',
          label: '访问路径',
          sortable: false,
          width: ''
        },
        {
          prop: 'name',
          label: '路径名称',
          sortable: false,
          width: ''
        },
        {
          prop: 'component',
          label: '前端组件',
          sortable: false,
          width: ''
        },
        {
          prop: 'sort',
          label: '排序',
          sortable: true,
          width: ''
        }
      ],
      userData: JSON.parse(sessionStorage.getItem('userData')),
      menuData: JSON.parse(sessionStorage.getItem('menus')),
      menuInfo: JSON.parse(sessionStorage.getItem('menuInfo')),
      drawerOpt: {
        title: '新增',
        dialog: false,
        direction: 'rtl',
        disabledSubmit: false
      },
      formData: null,
      visible: false
    }
  },
  methods: {
    addMenu() {
      if(!this.userData?.superAdmin) {
        return this.$message.error('无操作权限！')
      }
      this.formData = {
        type: 0,
        id: '',
        pid: '',
        parent: '',
        title: '',
        path: '',
        name: '',
        component: '',
        icon: '',
        sort: 0,
        disabled: false
      }
      this.drawerOpt.title = '新增'
      this.drawerOpt.disabledSubmit = false
      this.drawerOpt.dialog = true
    },
    handleEdit(row) {
      if(!this.userData?.superAdmin) {
        return this.$message.error('无操作权限！')
      }
      this.formData = {
        type: row.pid ? 1 : 0,
        parent: row.pid ? this.menuInfo.find(item => item.id === row.pid).title : '',
        ...row
      }
      this.drawerOpt.title = '编辑'
      this.drawerOpt.disabledSubmit = false
      this.drawerOpt.dialog = true
    },
    showDetails(row) {
      this.formData = {
        type: row.pid ? 1 : 0,
        parent: row.pid ? this.menuInfo.find(item => item.id === row.pid).title : '',
        ...row
      }
      this.drawerOpt.title = '详情'
      this.drawerOpt.disabledSubmit = true
      this.drawerOpt.dialog = true
    },
    addChildren(row) {
      if(!this.userData?.superAdmin) {
        return this.$message.error('无操作权限！')
      }
      this.formData = {
        type: 1,
        parent: row.title,
        id: '',
        pid: '',
        title: '',
        path: '',
        name: '',
        component: '',
        icon: '',
        sort: 0,
        disabled: false
      }
      this.drawerOpt.title = '添加子菜单'
      this.drawerOpt.disabledSubmit = false
      this.drawerOpt.dialog = true
    },
    deleteOne(row) {
      if(!this.userData?.superAdmin) {
        return this.$message.error('无操作权限！')
      }
      this.$confirm('确定要删除该菜单吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'fx-button-primary',
        cancelButtonClass: 'fx-button-default',
        type: 'warning'
      }).then(async () => {
        const res = await Sys_AdminService.deleteMenu(row.id)
        if (res.data.status !== 200) return this.$message.error(res.data.message)
        this.$message.success(row.title + res.data.message)
        setTimeout(() => {
          sessionStorage.removeItem('menus')
          this.$router.go(0)
        }, 1500)
      }).catch(() => {
      })
    },
    handleCommand(command) {
      switch (command.cmd) {
        case 'details':
          this.showDetails(command.row)
          break
        case 'addChildren':
          this.addChildren(command.row)
          break
        case 'deleteOne':
          this.deleteOne(command.row)
          break
        default:
          break
      }
    },
    closeDrawer() {
      this.drawerOpt.dialog = false
    }
  }
}
</script>

<style>

.el-message-box {
  width: 360px !important;
}

.el-message-box__status.el-icon-warning {
  color: red;
}

.fx-button-primary {
  color: #FFF;
  background-color: rgb(250, 84, 28) !important;
  border-color: rgb(234, 113, 72) !important;
  transition: .2s;
}

.fx-button-primary:hover {
  background-color: rgba(250, 84, 28, 0.8) !important;
  transition: .3s;
}

.el-button:active {
  color: rgb(250, 84, 28) !important;
  border-color: rgb(234, 113, 72) !important;
}

.fx-button-default {
  transition: .2s;
}

.fx-button-default:hover {
  color: rgba(250, 84, 28, 0.8);
  border-color: rgb(234, 113, 72);
  background-color: rgba(250, 84, 28, 0.1);
  transition: .3s;
}

.el-message-box__headerbtn:focus .el-message-box__close, .el-message-box__headerbtn:hover .el-message-box__close {
  color: rgba(250, 84, 28, 0.8);
}
</style>

<style scoped>
>>> .el-button--primary {
  color: #FFF;
  background-color: rgb(250, 84, 28);
  border-color: rgb(234, 113, 72);
  transition: .2s;
}

>>> .el-button--primary:hover {
  background-color: rgba(250, 84, 28, 0.8);
  transition: .3s;
}

>>> .el-button--default {
  transition: .2s;
}

>>> .el-button--default:hover {
  color: rgba(250, 84, 28, 0.8);
  border-color: rgb(234, 113, 72);
  background-color: rgba(250, 84, 28, 0.1);
  transition: .3s;
}

>>> .el-button--text {
  color: rgb(250, 84, 28);
  transition: .2s;
}

>>> .el-button--text:hover {
  color: rgba(250, 84, 28, 0.8);
  transition: .3s;
}

.el-dropdown-link {
  font-size: 12px;
  cursor: pointer;
  color: rgb(250, 84, 28);
}

.el-dropdown-link:hover {
  color: rgba(250, 84, 28, 0.8);
}

.el-icon-arrow-down {
  font-size: 12px;
}

.el-dropdown-menu__item:hover {
  color: rgba(250, 84, 28, 0.8);
}

>>> .el-table thead {
  color: rgba(0, 0, 0, 0.7);
}

>>> .el-popover {
  position: absolute;
  top: 100px !important;
}

>>> .el-select .el-input.is-focus .el-input__inner{
  border-color: rgba(250, 84, 28, 0.8);
}
</style>
